package cnsatmsmart.automation.mmi.view.layers;

import cnsatmsmart.CNSATMSMARTApp;
import cnsatmsmart.CNSATMSMARTView;
import cnsatmsmart.automation.mmi.controller.IO.ImageDriver;
import com.bbn.openmap.Layer;
import com.bbn.openmap.event.MapMouseListener;
import com.bbn.openmap.event.ProjectionEvent;
import com.bbn.openmap.event.SelectMouseMode;
import com.bbn.openmap.omGraphics.OMGraphicList;
import com.bbn.openmap.proj.Projection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author SIK - PTIK, BPPT
 */
public class ControlLayer extends Layer implements MapMouseListener{
    /**
     * Atributtes
     */
    private JPopupMenu PopUpMenu;             //Pop up menu on right click mouse event
    private String ScreenTitle; 
    private OMGraphicList graphics = new OMGraphicList();
    private Projection projection = null;
    private MouseEvent mouseClicked = null;
    
    /**
     * Konstruktor
     * @param screenTitle
     */
    public ControlLayer(String screenTitle)
    {
        this.ScreenTitle = screenTitle;
        
        /**
         * Pop Up Menu on right click mouse event
         */
        //<editor-fold defaultstate="colapsed" desc="Pop Up Menu">
        PopUpMenu = new JPopupMenu();
        JMenuItem item = new JMenuItem("Copy to clipboard");
        item.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            ImageDriver jpegsv = new ImageDriver(ScreenTitle, 200);
            Thread threadJPEG = new Thread(jpegsv);
            threadJPEG.start();
          }
        });
        PopUpMenu.add(item);

        JMenuItem item2 = new JMenuItem("Save as PNG");
            item2.addActionListener(
                new ActionListener() {
                    public void actionPerformed (ActionEvent e) {
                        CNSATMSMARTView view = (CNSATMSMARTView)CNSATMSMARTApp.getApplication().getMainView();
                        view.CloseTopDialog();
                        
                        JFileChooser fc = new JFileChooser();
                        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                        FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG files", "png");
                        fc.setFileFilter(filter);
                        int ret = fc.showSaveDialog(null);
                        view.ShowTopDialog();

                        if (ret == JFileChooser.APPROVE_OPTION)
                        {
                            File SelectedFile = fc.getSelectedFile();
                            String extension = getExtension(SelectedFile);
                            String fileName = SelectedFile.getAbsolutePath();

                            if (extension == null || !extension.toLowerCase().equals("png"))
                            {
                                //Jika file yang dipilih tidak memiliki ekstensi atau ekstensi salah
                                fileName += ".png";
                                File NewFile  = new File (fileName);
                                SelectedFile = NewFile;
                            }
                            ImageDriver PNGsv = new ImageDriver(ScreenTitle, SelectedFile, 200);
                            Thread threadPNG = new Thread(PNGsv);
                            threadPNG.start();
                        }
                    }
                }
            );
        this.PopUpMenu.add(item2);
        //</editor-fold>
    }

    /**
     * Fungsi untuk mengembalikan ekstensi dari suatu file
     * @param f File yang akan diketahui ekstensinya
     * @return Ekstensi dari file yang dimasukkan
     */
    private static String getExtension(File f){
            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');

            if (i > 0 &&  i < s.length() - 1) {
                ext = s.substring(i+1).toLowerCase();
            }
            return ext;
    }

    /**
     *
     * @param pe
     */
    public void projectionChanged(ProjectionEvent pe) {
        this.projection = (Projection) pe.getProjection().makeClone();
        this.graphics.project(this.projection, true);
        repaint();
    }

    /**
     *
     * @return
     */
    public String[] getMouseModeServiceList() {
        String[] RetVal = new String[1];
        RetVal[0] = SelectMouseMode.modeID; //gestures
        return (RetVal);
    }

    /**
     * Tidak diimplementasikan
     * @param me
     * @return
     */
    public boolean mousePressed(MouseEvent me) {
        return false;
    }

    /**
     * Tidak diimplementasikan
     * @param me
     * @return
     */
    public boolean mouseReleased(MouseEvent me) {
        return false;
    }

    /**
     * Fungsi ketika mouse diklik
     * @param me Mouse Event
     * @return
     */
    public boolean mouseClicked(MouseEvent me) {
        //Jika mouse yang diklik adalah button yang kanan, atau button 3 maka akan ditampilkan pop up menu
        if (me.getButton() == 3)
        {
            PopUpMenu.show(me.getComponent(), me.getX(), me.getY());
            return true;
        }
        
        return false;
    }

    /**
     * Tidak diimplementasikan 
     * @param me
     */
    public void mouseEntered(MouseEvent me) { }

    /**
     * Tidak diimplementasikan 
     * @param me
     */
    public void mouseExited(MouseEvent me) { }

    /**
     * Tidak diimplementasikan 
     * @param me
     * @return
     */
    public boolean mouseDragged(MouseEvent me) {
        return false;
    }

    /**
     * Tidak diimplementasikan 
     * @param me
     * @return
     */
    public boolean mouseMoved(MouseEvent me) {
        return false;
    }

    /**
     * Tidak diimplementasikan 
     */
    public void mouseMoved() { }
    
    /**
     *
     * @return
     */
    @Override
    public MapMouseListener getMapMouseListener () {
        return (this);
    }
}
